//获取百度相关搜索推荐
let arrText = [];
let baiduConfig = null;
// baiduConfig={
//     "arrText":"anMoYi",
//     "name":"按摩椅",
//     "pinYin":"anMoYiPin"
// }
// baiduConfig={
//     "arrText":"dianNaoYi",
//     "name":"电脑椅",
//     "pinYin":"dianNaoYi_Pin"
// }
// baiduConfig={
//     "arrText":"rongTai",
//     "name":"荣泰按摩椅",
//     "pinYin":"rongTai_Pin"
// }

// baiduConfig={
//     "arrText":"aokesi",
//     "name":"补血",
//     "pinYin":"buxue_Pin"
// }

// baiduConfig={
//     "arrText":"aokesi",
//     "name":"阿胶",
//     "pinYin":"ejiao_Pin"
// }
// baiduConfig={
//     "arrText":"aokesi",
//     "name":["高压锅","压力锅","欧乐多","苏泊尔"],
//     "pinYin":"gaoyaguo_Pin"
// }

// baiduConfig={
//     "arrText":"aokesi",
//     "name":["燕窝"],
//     "pinYin":"yanwo_Pin"
// }

// baiduConfig={
//     "arrText":"naifen",
//     "name":["高钙奶粉","补钙奶粉","羊奶粉","钙奶粉"],
//     "pinYin":"naifen_Pin"
// }

// baiduConfig={
//     "arrText":"benming",
//     "name":["本命年"],
//     "pinYin":"benming_Pin"
// }

// baiduConfig={
//     "arrText":"nanmu",
//     "name":["楠木"],
//     "pinYin":"nanmu_Pin"
// }

// baiduConfig={
//     "arrText":"buzhanguo",
//     "name":["不粘锅"],
//     "pinYin":"buzhanguo_Pin"
// }
// baiduConfig={
//     "arrText":"zhiniaoku",
//     "name":["纸尿裤","尿不湿",'纸尿片'],
//     "pinYin":"zhiniaoku_Pin"
// }
// baiduConfig={
//     "arrText":"zhichuang",
//     "name":["痔疮","坐垫"],
//     "pinYin":"zhichuang_Pin"
// }
// baiduConfig={
//     "arrText":"ranfa",
//     "name":["染发"],
//     "pinYin":"ranfa_Pin"
// }
// baiduConfig={
//     "arrText":"ranfa2",
//     "name":["孕妇在家染发","在家染发"],
//     "pinYin":"ranfa_Pin2"
// }

// baiduConfig={
//     "arrText":"kangzhou",
//     "name":["抗皱","除皱"],
//     "pinYin":"kangzhou_Pin"
// }

// baiduConfig={
//     "arrText":"tieguo",
//     "name":["铁锅"],
//     "pinYin":"tieguo_Pin"
// }

// baiduConfig = {
//     "arrText": "huajiao",
//     "name": ["花胶", "鱼胶"],
//     "pinYin": "huajiao_Pin"
// }

// baiduConfig = {
//     "arrText": "qingshang",
//     "name": ["高情商","打动"],
//     "pinYin": "qingshang_Pin"
// }

// baiduConfig = {
//     "arrText": "yaliwa",
//     "name": ["弹力袜","压力袜"],
//     "pinYin": "yaliwa_Pin"
// }

baiduConfig = {
    "arrText": "chajia",
    "name": ["浏览器插件","chrome插件","自动点击","js自动","浏览器自动","前端自动","自动化"],
    "pinYin": "chajia_Pin"
}

let getArrText = window.localStorage.getItem(baiduConfig.arrText);
arrText = getArrText ? JSON.parse(getArrText) : [];

arrText = arrText.filter(item => {
    if (containsMultipleSubstrings(item, baiduConfig.name)) {
        return item;
    }
})

function baiduSelectText() {

    $("#form .bdsug li").map((key, item) => {
        if (containsMultipleSubstrings(item.innerText, baiduConfig.name)) {
            arrText.push(item.innerText)
        }
    })
    console.log("百度输入框相关---33--");
    let arrTextTemp = arrayDeduplication(arrText); //数组去重
    arrTextTemp = arrTextTemp.filter(item => {
        if (containsMultipleSubstrings(item, baiduConfig.name)) {
            return item;
        }

    })
    console.log(arrTextTemp);
    creatJson(arrTextTemp)
    window.localStorage.setItem(baiduConfig.arrText, JSON.stringify(arrTextTemp))
}


function baiduText() {
    baiduHtml = "";
    $(".c-font-medium .c-gap-top-xsmall").text(function (index, element) {
        arrText.push(element)
    })

    $("#rs table th,#rs_new td").text(function (index, element) {
        arrText.push(element)
        // alert(element)
    })


    let arrTextTemp = arrayDeduplication(arrText); //数组去重
    window.localStorage.setItem(baiduConfig.arrText, JSON.stringify(arrTextTemp))

    console.log("百度相关数据", arrTextTemp);
    //console.log("\n\n<ul class='main-area'>" + baiduHtml + "</ul>\n\n");


}
function creatJson(dataArray) {

    computerIsHave(dataArray).then((value) => {
        let tempArrTextPin = value.arrTextPin.map((item, index) => {
            item.url = item.url + index;
            return item
        })
        window.localStorage.setItem(baiduConfig.pinYin, JSON.stringify(tempArrTextPin))
    })



}

function computerIsHave(dataArray) {
    return new Promise(function (resolve, reject) {
        let arrTextPin = [];
        let tempArr = [];
        let getArrTextPin = window.localStorage.getItem(baiduConfig.pinYin);
        arrTextPin = getArrTextPin ? JSON.parse(getArrTextPin) : [];
        arrTextPin = arrTextPin.filter((item, index) => {

            if (containsMultipleSubstrings(item.title, baiduConfig.name)) {
                return item.title;
            }

        })

        arrTextPin.map(item => {
            tempArr.push(item.title)
        })
        // 使用 includes 方法判断并添加
        dataArray.map(sub => {
            console.log("----dataArray.includes(sub.class)", dataArray.includes(sub))
            let notContain = !containsMultipleSubstrings(sub, ["退游", "线下", "玩", "315", "税", "醉", "专", "网", "拼音",
                "厂", "炖", "消费", "加盟", "退网", "逃", "画", "地铁", "知乎", "加盟店", "百度", "电影", "软件", "键盘", "免费",
                "图片", "游戏", "平台", "视频", "免费", "骗", "直播", "作文", "英文", "下载", "电话", "朋友圈", "关",
                "字", "医院", "图解", "小红书","已婚","阅读","pdf","PDF","电子","在线","书","二手","房价"]); //排除标题中含有的内容
            if (!tempArr.includes(sub) && containsMultipleSubstrings(sub, baiduConfig.name) && sub.length > 6 && notContain) {

                arrTextPin.push({
                    "title": sub,
                    "url": getCurrentDate(3)
                })
            }
        })
        resolve({ arrTextPin });
    })
}

//数组去重
function arrayDeduplication(arr) {
    return [...new Set(arr)];
}

/**
 *获取当前时间
 *format=1精确到天
 *format=2精确到分
 */
function getCurrentDate(format) {

    // let dateTime = new Date();
    // let timestampInMilliseconds = dateTime.getTime();

    let now = new Date(); // 获取当前时间
    let startOfYear = new Date(now.getFullYear(), 0, 1); // 获取当前年份开始的时间
    let timestampWithoutYear = now.getTime() - startOfYear.getTime();

    if (format == 3) {
        time = timestampWithoutYear;
    }

    return time;
}

function timeSet() {
    // 创建一个新的Date对象，它将包含当前的日期和时间
    const now = new Date();

    // 获取年月日时分秒
    const year = now.getFullYear();        // 年份
    const month = now.getMonth() + 1;      // 月份，getMonth()返回的月份是从0开始的，所以需要+1
    const day = now.getDate();            // 日
    const hour = now.getHours();          // 小时
    const minute = now.getMinutes();      // 分钟
    const second = now.getSeconds();      // 秒钟

    // 如果需要，可以添加前导零
    const formattedMonth = month.toString().padStart(2, '0');
    const formattedDay = day.toString().padStart(2, '0');
    const formattedHour = hour.toString().padStart(2, '0');
    const formattedMinute = minute.toString().padStart(2, '0');
    const formattedSecond = second.toString().padStart(2, '0');

    // 拼接字符串
    const dateTimeString = `${year}${formattedMonth}${formattedDay}${formattedHour}${formattedMinute}${formattedSecond}`;
    return dateTimeString;
    //console.log(dateTimeString); // 输出格式类似于 "2024-09-28 15:23:45"
}



function containsMultipleSubstrings(str, substrings) {
    return substrings.some(function (sub) {
        return str.indexOf(sub) !== -1;
    });
}



module.exports = { baiduText, baiduSelectText }